<?php

class My_Graph {

    function  __construct($id = null) {

        $this->tableName = 'graphs';
        $this->table = new Zend_Db_Table($this->tableName);
        
        $info = $this->table->info();
        $this->primary = $info['primary'][1];
        
        $this->request = new Zend_Controller_Request_Http;
        
        if(!empty($id)){
            $this->setId($id);
            $this->readById();
        }
    }

    function setId($id){
        $this->id = $id;
    }

    function getId(){
        return $this->id;
    }

    function exists(){
        return $this->data[$this->primary] ? true : false;
    }

    function readById($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        $result = $this->table->find($this->getId());
        $data = $result->toArray();
        $this->data = $data[0];
    }

    function readByName($name){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->where('name = ?', $name)
            );

        $data = $result->toArray();
        $this->data = $data[0];
    }

    function getAll(){
        $result = $this->table->fetchAll(
            $this->table->select()
                ->order('name')
            );
        return $result->toArray();
    }

    function getFirst($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['first'];
    }

    function getName($id = null){
        if(!empty($id)){
            $this->setId($id);
        }
        if(!$this->exists()){
            $this->readById();
        }
        return $this->data['name'];
    }

    function setData($data){
        $this->data = $data;
    }

}
?>
